// __PUBLIC__/js/page/admin/push/syspush
require(['../../common'], function() {
  require(['push.syspush'])
})

define('push.syspush', [
  'jquery',
  'css!ke/themes/default/default.css',
  'ke/kindeditor',
  'ke/lang/zh_CN',
  'jqp/validate'
], function($) {
  'use strict';

  $('#syspush_form').validate({
    classPlace: '.kv',
    errorPlace: '.kv-error',
    errors: {
      title: {
        required: '请填写推送标题',
      },
      content: {
        required: '请填写推送内容',
      },
    }
  })

  $(document).ready(function() {
    $('#select_user').click(function() {
		var url = APP + '?app=admin&m=User&a=userdialog'
	    var dialog = KindEditor.dialog({
	      width: 800,
	      height: 550,
	      title: '选择用户',
	      body: '<iframe id="user-frame" name="userFrame" style="width:100%;height:100%" frameborder="0" src="' + url + '"></iframe>',
	      closeBtn: {
	        name: '关闭',
	        click: function(e) {
	          dialog.remove();
	        }
	      },
	      yesBtn: {
	        name: '确定',
	        click: function(e) {
	          var ids = new Array()
	          $('#user-frame').contents().find('tbody').find(':checked').each(function() {
	        	ids.push($(this).closest('tr').data('value'))
	          })
	          var oldval = new Array()
	          var i = 0
	          $('input[name^="select_users"]').each(
	            function() {
	              oldval[i] = $(this).val()
	              i++
	            }
	          )
	          var html = ''
	          for (var i = 0; i < ids.length; i++) {
	            var val = ids[i].split('_')
	            if ($.isArray(oldval) && oldval.length > 0) {
	              if ($.inArray(val[0], oldval) > -1) {
	                continue
	              }
	              html += '<p>'
	              html += '<input type="hidden" name="select_users[]" value="' + val[0] + '"/>'
	              html += '<a href="javascript:;" class="A_ico_top">' + val[1] + '</a>&nbsp;&nbsp; '
	              html += '</p>'
	            } else {
	              html += '<p>'
	        	  html += '<input type="hidden" name="select_users[]" value="' + val[0] + '"/>'
	              html += '<a href="javascript:;" class="A_ico_top">' + val[1] + '</a>&nbsp;&nbsp; '
	              html += '</p>'
	            }
	          }
	          $('#select_userlist_div').append(html)
	          dialog.remove()
	        }
	      },
	      noBtn: {
	        name: '取消',
	        click: function(e) {
	          dialog.remove()
	        }
	      }
	    });
    });
  });

  $(document).on('change', '#roleid', function(e) {
    e.preventDefault()
    var id = $('#roleid').val()
    if (id == -1) {
      $('#select_user').show()
    } else {
      $('#select_user').hide()
      $('#select_userlist_div').html('')
    }
  })
    .on('click', '.A_ico_top', function(e) {
      e.preventDefault()
      $(this).parent().remove()
    })
    .on('click', '#J_do', function(e) {
    	syspush()
    })

  function syspush() {
    var id = $('#roleid').val();
    var title = $('#title').val();
    var content = $('#content').val();
    var userlist = new Array();
    if (id == -1) {
      var i = 0;
      $('input[name^="select_users"]').each(
        function() {
          userlist[i] = $(this).val();
          i++;
        }
      )
      if (userlist.length < 1) {
        alert('请选择用户');
        return false;
      }
    }
    $.post(APP + '?app=admin&m=Push&a=dosyspush', {
      id: id,
      title: title,
      content: content,
      userlist: userlist
    }, function(res) {
      alert(res);
    });
  }
})
